package com.tigercel.xiangchengyun.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.entity.HotVideoEntity
import com.tigercel.xiangchengyun.ui.activities.VideoAndWebActivity


/**
 * Created by wqj on 2018/5/26.
 */
class HotVideoAdapter(private val mContext: Context, private val mHotVideoEntityList: MutableList<HotVideoEntity>) : BaseAdapter() {

    override fun getCount(): Int {
        return mHotVideoEntityList.size
    }

    override fun getItem(position: Int): Any {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val viewHolder: ViewHolder
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_hot_video, null)
            viewHolder = ViewHolder()
            viewHolder.title = convertView!!.findViewById(R.id.videoname)
            viewHolder.mImageView = convertView.findViewById(R.id.videoimage)
            viewHolder.content = convertView.findViewById(R.id.li_content)
            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }
        viewHolder.title!!.text = mHotVideoEntityList[position].title
        Log.e("图片地址", mHotVideoEntityList[position].thumb_img)
        Glide.with(mContext)
                .load(mHotVideoEntityList[position].thumb_img)
                .into(viewHolder.mImageView!!)
        viewHolder.content!!.setOnClickListener {
            var intent = Intent()
            intent.putExtra("videourl", mHotVideoEntityList[position].url)
            intent.putExtra("url", "https://www.baidu.com/")
            intent.putExtra("title", mHotVideoEntityList[position].title)
            intent.setClass(mContext, VideoAndWebActivity::class.java)
            mContext.startActivity(intent)

        }
        return convertView
    }

    internal class ViewHolder {
        var title: TextView? = null
        var mImageView: ImageView? = null
        var content: LinearLayout? = null
    }

    fun addAll(data: MutableList<HotVideoEntity>) {
        mHotVideoEntityList.addAll(data)
        notifyDataSetChanged()
    }

    fun clear() {
        mHotVideoEntityList.clear()
        notifyDataSetChanged()
    }
}
